﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;
using JasRibbonApplicatie.Classes;
using JasRibbonApplicatie.Model;
using JasRibbonApplicatie.UserControls;
using System.Threading.Tasks;
using System.IO;

namespace JasRibbonApplicatie.Views.BeheerAanvragen
{
    /// <summary>
    /// Interaction logic for MailWindow.xaml
    /// </summary>
    public partial class MailWindow : RibbonWindow
    {

        private static GebruikerModel gm = new GebruikerModel();

        public MailWindow(string to="", string subject="", string body="", List<FileInfo> attachments=null, string cc="", string bcc="")
        {
            InitializeComponent();
            
            aanTextBox.Text = to;
            ccTextBox.Text = cc;
            bccTextBox.Text = bcc;
            onderwerpTextbox.Text = subject;

            BodyEditor.ContentHtml = body;

            if (attachments != null)
            {
                foreach (FileInfo fi in attachments)
                {
                    Attachment fileitem = new Attachment();
                    fileitem.File = fi;
                    fileitem.removeClick += new RoutedEventHandler(attachment_removeClick);
                    BijlageGroup.Items.Add(fileitem);
                }
            } 
        }
        private void hideCcBccButton_Click(object sender, RoutedEventArgs e)
        {
            if (hideCcBccButton.Content.ToString() == "Cc Bcc verbergen")
            {
                ccLabel.Visibility = Visibility.Collapsed;
                ccTextBox.Visibility = Visibility.Collapsed;
                bccTextBox.Visibility = Visibility.Collapsed;
                bccLabel.Visibility = Visibility.Collapsed;
                hideCcBccButton.Content = "Cc Bcc weergeven";
            }
            else
            {
                ccLabel.Visibility = Visibility.Visible;
                ccTextBox.Visibility = Visibility.Visible;
                bccTextBox.Visibility = Visibility.Visible;
                bccLabel.Visibility = Visibility.Visible;
                hideCcBccButton.Content = "Cc Bcc verbergen";
            }
        }


        private void aanLabel_Click(object sender, RoutedEventArgs e)
        {
            emailPopup.VerticalOffset = 25;
            emailPopup.IsOpen = true;
            toLabel.Content = "Aan:";
            searchTextBox.Focus();
        }
        private void ccLabel_Click(object sender, RoutedEventArgs e)
        {
            emailPopup.VerticalOffset = 50;
            emailPopup.IsOpen = true;
            toLabel.Content = "CC:";
            searchTextBox.Focus();
        }
        private void bccLabel_Click(object sender, RoutedEventArgs e)
        {
            emailPopup.VerticalOffset = 75;
            emailPopup.IsOpen = true;
            toLabel.Content = "BCC:";
            searchTextBox.Focus();
        }
        private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (searchTextBox.Text.Count() > 0)
            {
                List<EmailAdres> emails = gm.GetMailAddresses(searchTextBox.Text);
                resultsListBox.ItemsSource = emails;
            }

            okButton.IsEnabled = (resultsListBox.SelectedItems.Count > 0);
        }
        private void resultsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            okButton.IsEnabled = (resultsListBox.SelectedItems.Count > 0);
        }
        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            string output = "";
            emailPopup.IsOpen = false;
            if (resultsListBox.SelectedItems.Count > 0)
                foreach (EmailAdres i in resultsListBox.SelectedItems)
                    output += i.Naam + " <" + i.Email + ">;";

            switch (toLabel.Content.ToString())
            {
                case "Aan:":
                    aanTextBox.Text += output;
                    break;
                case "CC:":
                    ccTextBox.Text += output;
                    break;
                case "BCC:":
                    bccTextBox.Text += output;
                    break;
            }
        }

        private void verzendButton_Click(object sender, RoutedEventArgs e)
        {
            List<string> filenames = new List<string>();
            if (BijlageGroup.Items.Count > 0)
                foreach (Attachment at in BijlageGroup.Items)
                    filenames.Add(at.File.FullName);

            string recipient = aanTextBox.Text;
            string subject = onderwerpTextbox.Text;
            string html = BodyEditor.ContentHtml;
            string cc = ccTextBox.Text;
            string bcc = bccTextBox.Text;

            Task.Factory.StartNew(() => SendMail(recipient, subject, html, filenames.ToArray(), cc, bcc));
            
        }
        
        private void attachment_removeClick(object sender, RoutedEventArgs e)
        {
            BijlageGroup.Items.Remove(sender as Attachment);
        }

        private void BijlageGroup_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];
                foreach (string droppedFilePath in droppedFilePaths)
                {
                    Attachment fileitem = new Attachment();
                    fileitem.File = new FileInfo(droppedFilePath);
                    fileitem.removeClick += new RoutedEventHandler(attachment_removeClick);
                    BijlageGroup.Items.Add(fileitem);
                }
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        
        private void SendMail(string aan, string onderwerp, string body, string[] files, string cc, string bcc)
        {
            try
            {
                Emailer emailer;
                emailer = new Emailer(Properties.Settings.Default.SMTP_HOST, Properties.Settings.Default.SMTP_PORT, Properties.Settings.Default.SMTP_USER, Properties.Settings.Default.SMTP_PASS);
                emailer.From = new System.Net.Mail.MailAddress(Properties.Settings.Default.EMAIL_ADDRESS, Properties.Settings.Default.EMAIL_NAME);
                emailer.SendMessage(aan, onderwerp, body, files, cc, bcc);
            }
            catch (Exception ex)
            {
                Console.WriteLine("MailWindow.cs SendMail exeption:" + ex.ToString());
            }
        }

    }
}
